package ${packageName}.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.common.JsonResult;
import ${packageName}.vo.${className};
import ${packageName}.service.${className}Service;
 
/**
 * 
 * <br>
 * <b>功能：</b>${className}Controller<br>
 * <b>作者：</b>suxy<br>
 * <b>日期：</b>${.now} <br>
 * <b>版权所有：
 */ 
@Controller
@RequestMapping("/${className?uncap_first}") 
public class ${className}Controller {
	
	private final static Logger log= LoggerFactory.getLogger(${className}Controller.class);
	
	@Autowired
	private ${className}Service ${className?uncap_first}Service; 
	
	/**
	 * 列表
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(ModelMap model,${className} ${className?uncap_first}) throws Exception{
		List<${className}> dataList = ${className?uncap_first}Service.query(${className?uncap_first});
		Integer total = ${className?uncap_first}Service.query${className}Count(${className?uncap_first});
		model.put("${className}s", dataList);
		${className?uncap_first}.setTotal(total);
		model.put("${className?uncap_first}", ${className?uncap_first});
		return new ModelAndView("list_${className}_view");
	}
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Object save(ModelMap model,${className} entity) throws Exception{
	<#list attrs as a>
		<#if a.primary?contains("TRUE")>
			if(entity.get${a.columnName?cap_first}()==null){
				${className?uncap_first}Service.insert(entity);
			}else{
				${className?uncap_first}Service.update(entity);
			}
		</#if> 
	</#list>
		return new ModelAndView(new RedirectView("list.htm"));
	}
	
	@RequestMapping("/getId")
	public Object getId(ModelMap model,${className} ${className?uncap_first}) throws Exception{
		${className} entity  = ${className?uncap_first}Service.queryById(${className?uncap_first});
		model.put("${className}", entity);
		if("e".equals(${className?uncap_first}.getA())){
			return new ModelAndView("edit_${className}_view");
		}
		return new ModelAndView("view_${className}_view");
	}
	
	
	
	@RequestMapping("/delete")
	public Object delete(String id) throws Exception{
		${className?uncap_first}Service.delete(id);
		return new JsonResult("删除成功");
	}

	@InitBinder  
   protected void initBinder(WebDataBinder binder) {  
		 binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));  
   } 
   
}
